package com.bdqn.service;

import com.bdqn.mapper.SysUserMapper;
import com.bdqn.pojo.SysUser;
import com.bdqn.util.PageSupport;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserMapper sysUserMapper;


    /**
     * 根据用户ID删除用户
     *
     * @param uploadPath
     * @param userId
     * @return -1表示不存在，1表示成功，0表示失败
     */
    @Override
    public int del(String uploadPath, Long userId) {
        // 查找用户是否存在（不存在时返回-1）
        SysUser sysUser = this.getById(userId);
        if (sysUser == null) {
            return -1;
        }
        // 先删除用户上传的身份证图片数据
        String idPicPath = sysUser.getIdPicPath();
        if (idPicPath != null) {
            idPicPath = idPicPath.split("/")[3];
            File file = new File(uploadPath + "/" + idPicPath);
            boolean delete = file.delete();
            System.out.println("[" + file.getPath() + "]资源删除" + (delete ? "成功" : "失败"));
        }
        // 先删除用户上传的工作证图片数据
        String workPicPath = sysUser.getWorkPicPath();
        if (workPicPath != null) {
            workPicPath = workPicPath.split("/")[3];
            File file = new File(uploadPath + "/" + workPicPath);
            boolean delete = file.delete();
            System.out.println("[" + file.getPath() + "]资源删除" + (delete ? "成功" : "失败"));
        }

        // 存在则删除
        return sysUserMapper.del(userId);
    }

    @Override
    public int getPwd(String oldPassword, String account) {
        return sysUserMapper.getPwd(oldPassword,account);
    }

    @Override
    public int getAccountC(String account) {
        return sysUserMapper.getAccountC(account);
    }

    @Override
    public boolean add(SysUser sysUser) {
        sysUserMapper.add(sysUser);
        return true;
    }

    @Override
    public Long count(String realName, Long roleId) {
        return sysUserMapper.count(realName,roleId);
    }

    @Override
    public List<SysUser> page(String realName, Long roleId, PageSupport page) {
        Long pageSize =  page.getPageSize();
        Long pageIndex =  ((page.getCurrentPageNo()-1)*pageSize);
        return sysUserMapper.page(realName,roleId,pageIndex,pageSize);
    }

    @Override
    public SysUser login(String account, String password) {
        if (account == null || password == null) {
            return null;
        }
        return sysUserMapper.getByAccountAndPassword(account,password);
    }

    @Override
    public boolean userExist(String account) {
        if (account == null) {
            return false;
        }
        return sysUserMapper.exist(account) == 1;
    }

    @Override
    public int savePassword(String account, String pwd) {
        return sysUserMapper.savePassword(account, pwd);
    }


    /**
     * 根据ID查询单个用户
     *
     * @param userId
     * @return
     */
    @Override
    public SysUser getById(Long userId) {
        if (userId == null) {
            return null;
        }
        return sysUserMapper.getById(userId);
    }
}
